#ifndef BALANCER_H
#define BALANCER_H

#include "worker.h"
#include "../utils/message.h"
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

class AppBalancer : public Thread {
public:
	virtual ~AppBalancer();
	void run();
	static AppBalancer* createInstance(string pConfigPath);
	static AppBalancer* getInstance();
	void registerFailedSubmission(string pSubmissionId);
	string getGraderPath();
	
protected:
	void setup();
	void execute();
	
private:
	static AppBalancer* sInstance;

	AppBalancer(string pConfigPath);
	AppBalancer(string pPath , int nWorker , string pLogFilePath , int pChannelId , int pMessageType);

	void init();
	void prepare();
	static void sighandler(int signal);

	bool mReady;
	string mGraderPath;
	ofstream mLogFile;
	int nWorker;
	vector<NewWorker*> mWorkers;
	int mChannelId;
	int mMessageType;
	MessageChannel *mChannel;
	
	vector<string> mUrl;
	queue<string> mSubmissionQueue;
};

#endif
